using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SoinusLibrary.Log;

namespace HomeLite.Controllers
{
    public class MonitorController
    {
        #region Fields
        private static MonitorController _instance;
        private List<FileSystemWatcher> _listfsWathchers;
        #endregion

        #region Event Handlers
        #endregion

        #region Properties
        public static MonitorController Instance
        {
            get
            {
                if (_instance == null) _instance = new MonitorController();
                return _instance;
            }
        }
        #endregion

        #region Constructor | Dispose
        private MonitorController()
        {
        }
        internal void Dispose()
        {
            this.Stop();
        }
        #endregion

        #region Methods - Private
        private void CreateWatchers()
        {
            _listfsWathchers = new List<FileSystemWatcher>();
            List<string> folders = FileController.Instance.GetFolders();
            foreach (string folder in folders)
            {
                this.AddWatcher(folder);
            }
        }
        private string GetFolderFromPath(string fullPath)
        {
            return fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
        }
        #endregion

        #region Methods
        internal void AddWatcher(string folder)
        {
            try
            {
                FileSystemWatcher filesWatcher = new FileSystemWatcher();
                filesWatcher.Path = folder;
                filesWatcher.Filter = @"*.mp3";
                filesWatcher.Created += new FileSystemEventHandler(FileCreated);
                filesWatcher.Deleted += new FileSystemEventHandler(FileDeleted);
                filesWatcher.Renamed += new RenamedEventHandler(FileRenamed);
                filesWatcher.EnableRaisingEvents = true;
                _listfsWathchers.Add(filesWatcher);
            }
            catch (Exception)
            {
                FileController.Instance.DeleteFolder(folder);
            }
        }
        internal void RemoveWatcher(string folder)
        {
            FileSystemWatcher fileWatcher = null;

            foreach (FileSystemWatcher f in _listfsWathchers)
            {
                if (f.Path == folder)
                {
                    fileWatcher = f;
                }
            }
            if (fileWatcher != null)
            {
                _listfsWathchers.Remove(fileWatcher);
                fileWatcher.Dispose();
            }
        }
        internal void Restart()
        {
            this.Stop();
            this.Start();
        }
        internal void Start()
        {
            if (_listfsWathchers == null)
            {
                this.CreateWatchers();
            }
        }
        internal void Stop()
        {
            if (_listfsWathchers != null)
            {
                foreach (FileSystemWatcher watcher in _listfsWathchers)
                {
                    watcher.EnableRaisingEvents = false;
                    watcher.Dispose();
                }
                _listfsWathchers.Clear();
                _listfsWathchers = null;
            }
        }
        #endregion

        #region Events
        void FileCreated(object sender, FileSystemEventArgs e)
        {
            string folder;
            string file;

            folder = this.GetFolderFromPath(e.FullPath);
            file = e.Name;

            if (!FileController.Instance.AddFile_R(folder,file))
            {
            }
        }
        void FileDeleted(object sender, FileSystemEventArgs e)
        {
            string folder;
            string file;

            folder = this.GetFolderFromPath(e.FullPath);
            file = e.Name;

            if (!FileController.Instance.DeleteFile_R(folder,file))
            {
            }
        }
        void FileRenamed(object sender, RenamedEventArgs e)
        {
            string oldFolder;
            string newFolder;
            string oldFile;
            string newFile;

            oldFolder = this.GetFolderFromPath(e.OldFullPath);
            newFolder = this.GetFolderFromPath(e.FullPath);
            oldFile = e.OldName;
            newFile = e.Name;


            if(FileController.Instance.DeleteFile_R(oldFolder,oldFile))
            {
                if(!FileController.Instance.AddFile_R(newFolder,newFile))
                {
                }
            }
            else
            {
            }
        }
        #endregion
    }
}
